package com.iweb.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {
    private static String QUEUE_A = "direct.queue.a";
    private static String QUEUE_B = "direct.queue.b";
    private static String EXCHANGE_NAME = "direct.exchange";
    private static String EXCHANGE_TYPE = "direct";

    public static void main(String[] args) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("122.112.160.198");
        factory.setUsername("guest");
        factory.setPassword("guest");

        // 获取会话
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_A, false, false, false, null);
        channel.queueDeclare(QUEUE_B, false, false, false, null);
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

        // 通过 routing-key 将队列和交换机进行绑定,每个队列可以绑定多个路由键
        channel.queueBind(QUEUE_A, EXCHANGE_NAME, "vegetables");// 青菜、空心菜、黄瓜
        channel.queueBind(QUEUE_A, EXCHANGE_NAME, "fruit");// 西瓜、苹果
        channel.queueBind(QUEUE_B, EXCHANGE_NAME, "meat");// 牛肉、羊肉
        // 发送数据
        channel.basicPublish(EXCHANGE_NAME, "vegetables", null, "青菜".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "vegetables", null, "空心菜".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "vegetables", null, "黄瓜".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "fruit", null, "西瓜".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "fruit", null, "苹果".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "meat", null, "牛肉".getBytes());
        channel.basicPublish(EXCHANGE_NAME, "meat", null, "羊肉".getBytes());

    }
}
